package ce.com.newbluedemo.k2;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import java.util.ArrayList;

import ce.com.cenewbluesdk.entity.k2.K2_SendContacts;
import ce.com.cenewbluesdk.proxy.BleFactory;
import ce.com.cenewbluesdk.proxy.CEDevK2Proxy;
import ce.com.newbluedemo.R;

/**
 * Created by YaoBinkun on 2016/12/30.
 */

public class ActivitySendContact extends K2BaseActivity implements View.OnFocusChangeListener {
    private String mContactName;
    private String mContactNum;
    private EditText mEtName;
    private EditText mEtPhone;
    private CEDevK2Proxy mK2Proxy;
    ArrayList<K2_SendContacts> contactses;
    private Handler mK2Handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            if (msg.arg1 == 0){
                showToast("发送失败，请稍后重试！");
            } else {
                showToast("发送成功！");
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_send_contact);
        setTitle("发送联系人");
        mK2Proxy = BleFactory.getInstance().getK2Proxy();
        mK2Proxy.setHandler(mK2Handler);
        contactses = new ArrayList<>();
        mEtName = (EditText) findViewById(R.id.et_contact_name);
        mEtPhone = (EditText) findViewById(R.id.et_contact_phone_num);
        mEtName.setOnFocusChangeListener(this);
        mEtPhone.setOnFocusChangeListener(this);
        findViewById(R.id.btn_send_contact).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!mK2Proxy.isConnectOK()) {
                    showToast("蓝牙已经断开");
                    return;
                }
                K2_SendContacts contacts = new K2_SendContacts();
                contacts.setName(mContactName);
                contacts.setPhoneNember(mContactNum);
                contactses.add(contacts);
                mK2Proxy.getSendHelper().sendPhoneBook(contactses, 0);
                mEtName.setText("");
                mEtPhone.setText("");
            }
        });
    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus) return;
        switch (v.getId()) {
            case R.id.et_contact_name:
                if (mEtName.getText().toString().isEmpty()) {
                    showToast("姓名不能为空");
                }
                mContactName = mEtName.getText().toString();
                break;
            case R.id.et_contact_phone_num:
                if (mEtPhone.getText().toString().isEmpty()) {
                    showToast("号码不能为空");
                }
                mContactNum = mEtPhone.getText().toString();
                break;
            default:
                break;
        }
    }

    private void showToast(String input) {
        Toast.makeText(this, input, Toast.LENGTH_SHORT).show();
    }
}
